/**
 * 
 *
 * 
 * 
 */
package com.east196.xfast.engine;

import java.io.IOException;

import com.east196.xfast.config.XfastConfig;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;

import com.east196.xfast.common.utils.xx;

/**
 * Eova表达式动态解析器
 * 变量+逻辑运算
 * @author East196
 *
 */
public class DynamicParse {

	public static GroupTemplate gt;
	
	static{
		if (gt == null) {
			StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
			Configuration cfg = null;
			try {
				cfg = Configuration.defaultConfiguration();
			} catch (IOException e) {
				e.printStackTrace();
			}
			gt = new GroupTemplate(resourceLoader, cfg);
		}
	}

	public static String buildSql(String exp, Object... params) {
		if (xx.isEmpty(exp) || xx.isEmpty(params)) {
			return exp;
		}
		exp = exp.replace("\n", "");
		exp = exp.replace("\r", "");
		exp = exp.replace("\t", " ");
		
		Template t = gt.getTemplate(exp);
		for(Object o : params){
			t.binding(o.getClass().getSimpleName().toLowerCase(), o);
		}
		
		exp = t.render();

		if (XfastConfig.isDevMode) {
			// System.out.println("XfastExp Build:" + exp);
		}

		return exp;
	}

}
